<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Equipment Config</title>
	<link rel="stylesheet" href="../js/easyui/themes/bootstrap/easyui.css">
	<link rel="stylesheet" href="../js/easyui/themes/icon.css">
	<link rel="stylesheet" href="../css/index-ui.css">
	<script type="text/javascript" src="../js/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/ams.util.js"></script>
	<script type="text/javascript" src="../js/webapi.js"></script>
	<script type="text/javascript" src="../js2/loginCookies.js"></script>
</head>
<body onload="page_init()">
	<div id="oplog_filter" style="padding:5px;padding-left:1px;padding-right:20px">
		<div style="float:left;padding-left:5px">
			<a class="easyui-linkbutton" style="width:120px" iconCls="icon-refresh" onclick="on_refresh_log_files()">Refresh List</a>
		</div>
		<div style="float:left;padding-left:10px">
			<a class="easyui-linkbutton" style="width:120px" iconCls="icon-remove" onclick="on_oplog_file_delete()">Delete File</a>
		</div>
		<div style="float:right;padding-left:30px">
			<a class="easyui-linkbutton" style="width:120px" iconCls="icon-search" onclick="on_refresh_log_content()">Refresh Logs</a>
		</div>
		<!--div style="float:right;padding-left:30px">
			<a class="easyui-linkbutton" style="width:120px" iconCls="icon-search" onclick="on_refresh_log_content()">Query</a>
		</div>
		<div style="float:right;padding-left:30px">
			<div style="float:left;padding-top:6px;padding-right:30px"><label> Time Filter: </label></div>
			<input id="filter_start_date" class="easyui-datebox" style="width:115px">
			<label> ---- </label>
			<input id="filter_end_date" class="easyui-datebox" style="width:115px">
		</div -->
	</div>
	<br/><br/>
	<div id="rcp_layout_main" class="easyui-layout cls_rcp_layout_main">
		<div data-options="region:'west',split:true" title="" style="width:265px;">
			<table id="eq_logfile_list_grid" class="easyui-datagrid" title="" style="width:100%;height:80%"
				pagination="false">
				<thead>
					<tr>
						<th data-options="field:'f',width:250,align:'left'">Files</th>
					</tr>
				</thead>
			</table>
		</div>>
		<div data-options="region:'center',split:true" title="" style="width:400px;">
			<table id="eq_oplog_grid" class="easyui-datagrid" title="" style="width:100%;height:80%"
				pagination="false">
				<thead>
					<tr>
						<!--th data-options="field:'no',width:40">No</th-->
						<th data-options="field:'ts',width:140">Time</th>
						<th data-options="field:'type',width:140,align:'left'">Type</th>
						<th data-options="field:'cnt',width:360,align:'left'">Contents</th>
					</tr>
				</thead>
			</table>
		</div>
	</div>
	<input type="hidden" id="FileName">
	<!--
	-->
</body>

<script language="javascript"> 
var eqn = null;
function page_init()
{
	eqn = get_page_param('eq')
	// left list
	$('#eq_logfile_list_grid').datagrid({
		onSelect: function(rowIndex, rowData) {
			console.log(rowIndex)
			console.log(rowData.f)
			$("#FileName").val(rowData.f)
		//	alert('click file: ' + row.f)
			on_refresh_log_content()
		}
	})

	on_refresh_log_files()

	// log data
	/*
	var tmpday = new Date()
	var end_day = tmpday.getMonth() + 1 + '/' + tmpday.getDate() + '/' + tmpday.getFullYear()
	tmpday.setDate(tmpday.getDate() - 1)
	var start_day = tmpday.getMonth() + 1 + '/' + tmpday.getDate() + '/' + tmpday.getFullYear()
	
	$('#filter_start_date').datebox('setValue', start_day)
	$('#filter_end_date').datebox('setValue', end_day)
	*/

//	query_data()
}

function on_refresh_log_files()
{

	 $('#eq_logfile_list_grid').datagrid({
	//	rownumbers:'true',
		showHeader:false,
		singleSelect:'true',
		lines:true,
		clickToEdit: false,
		dblclickToEdit: false,
		loadMsg: 'Loading Data ......',
		method:'get'
	})

	if (ams_is_sim_mode()) {
		$.getJSON('oplog_files_01.json', function(json_data) {
			$('#eq_logfile_list_grid').datagrid({
				data:json_data.rows
			})
		})
	} else {
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getEqOpLogNames"),	
			data:JSON.stringify({"equipId":eqn,"index":0,"count":10}),
			dataType: 'json',
			success : function(datas) {
				// console.log(getData())
				console.log(JSON.parse(datas.d))
				$('#eq_logfile_list_grid').datagrid('loadData',JSON.parse(datas.d));
				// if(JSON.parse(datas.d).length==10){
	//              count+=10;
	//              $('#pp').pagination({
				// 		total:count
				// 	});
				// }
				// console.log(count)
				
			},
			error: function (error) {
				//alert(errorThrown.message);
				console.log(error) 
			}
		});
	}
    
	
/*
	$('#eq_logfile_list_grid').datagrid('getPager').pagination({
		showPageList: false,
        showRefresh: false,
        displayMsg: ''
	})	*/
}

function on_oplog_file_delete()
{
	if (checkUserLevel() == false){
		return;
	}
	
	var deleteFileName = $('#eq_logfile_list_grid').datagrid('getSelected').f
	console.log(deleteFileName)
	$.messager.confirm('Confirm', 'Are you sure you want to delete it?', function(r){
		if (r){
		    $.ajax({
				type: 'POST',
				contentType: 'application/json',
				url: getWebServiceUrl("cmdEqOpLogs"),	
				data:JSON.stringify({"equipId":eqn,"name":deleteFileName,"cmd":1}),
				dataType: 'json',
				success : function(datas) {
					// console.log(getData())
					console.log(datas.d)
					if(datas.d == "0"){
						on_refresh_log_files()
						$('#eq_oplog_grid').datagrid('loadData',{total:0,rows:[]});
					}else if(datas.d == "-1"){
						alert("Delete failed.");
					}
					
				},
				error: function (error) {
					//alert(errorThrown.message);
					console.log(error) 
				}
			});
		}
	});
	

}

function on_refresh_log_content()
{
	var fileName = $("#FileName").val();
	console.log(fileName)
		// var t1 = $('#filter_start_date').datebox('getValue')
		// var t2 = $('#filter_end_date').datebox('getValue')
		// console.log(t1 + '---' + t2)
	$('#eq_oplog_grid').datalist('loadData',[])
//	alert(window.location.host)
	// var query_url = ''
	$('#eq_oplog_grid').datagrid({
		rownumbers:'true',
		singleSelect:'true',
		lines:true,
		clickToEdit: false,
		dblclickToEdit: false,
		loadMsg: 'Loading Data ......',
		method:'get'
		});
	if (ams_is_sim_mode()) {
	 	$.getJSON('oplog_01.json', function(json_data) {
			$('#eq_oplog_grid').datagrid({
				data:json_data.rows
			})
		 })
	} else {
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getEqOpLogs"),	
			data:JSON.stringify({"equipId":eqn,"name":fileName,"index":0,"count":10}),
			dataType: 'json',
			success : function(datas) {
				// console.log(getData())
				console.log(JSON.parse(datas.d))
				$('#eq_oplog_grid').datagrid('loadData',JSON.parse(datas.d));
				// if(JSON.parse(datas.d).length==10){
	//              count+=10;
	//              $('#pp').pagination({
				// 		total:count
				// 	});
				// }
				// console.log(count)
				
			},
			error: function (error) {
				//alert(errorThrown.message);
				console.log(error) 
			}
		});
	}
	// 	var row = $(this).datagrid('getSelected')
	// 	query_url = '....../getOpLog.aspx?file=' + row.f // TODO
	// }
}

</script>
	
</html>
